package com.chenjun.web.controller.oa;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.common.util.ExcelUtil;
import com.chenjun.common.util.ReadExcel;
import com.chenjun.core.dao.generator.entity.OaDictNotStdManHoursCfg;
import com.chenjun.core.dao.generator.entity.SysUser;
import com.chenjun.core.service.oa.OaDictNotStdManHoursCfgService;
import com.chenjun.core.service.sys.SysUserService;

/**
 * 时间录录
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("oahour")
public class OaUploadhourController {
	
	@Autowired
	private OaDictNotStdManHoursCfgService oaDictNotStdManHoursCfgService;
	
	@Autowired
	private SysUserService sysUserService;

	@RequestMapping("/list.html")
	public String index() {
		return "scheduleManagement/employeeScheduleImport";
	}
	
	@RequestMapping("/travellist.html")
	public String businessTripImport() {
		return "scheduleManagement/businessTripImport";
	}
	
	/**
	 * 上传文件
	 * 
	 * @param request
	 * @param response
	 * @param file
	 */
	@RequestMapping("/uploadfile")
	@ResponseBody
	public Object uploadfile(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "qqfile", required = true) MultipartFile file) {
		ResultDto rtDto = new ResultDto();
		try {
			if (!file.isEmpty()) {
				byte[] bytes = file.getBytes();
				String upath = request.getSession().getServletContext()
						.getRealPath("/");
				String uuid = UUID.randomUUID().toString();
				String path = "upload/" + uuid + file.getOriginalFilename();
				FileOutputStream fos = new FileOutputStream(upath + path);
				fos.write(bytes);
				fos.close();
				rtDto.setType(rtDto.SUCCESS);
				rtDto.setData(path);
				return rtDto;
			} else {
				rtDto.setType(rtDto.ERROR);
				rtDto.setMessage("文件不存在");
				return rtDto;
			}
		} catch (Exception e) {
			rtDto.setType(rtDto.ERROR);
			rtDto.setMessage("上传失败");
			return rtDto;
		}
	}

	
	@RequestMapping("/deletfile")
	@ResponseBody
	public Object deletFile(HttpServletRequest request,
			HttpServletResponse response,String path){
		String upath = request.getSession().getServletContext()
				.getRealPath("/");
		String filepath=upath+path;
		
		File file=new File(filepath);
		boolean flag=false;
		ResultDto rtDto = new ResultDto();
		if(file ==null){
			rtDto.setType(rtDto.ERROR);
			rtDto.setMessage("文件不存在");
			return rtDto;
		}
		flag = file.delete();
		if(!flag){
			rtDto.setType(rtDto.ERROR);
			rtDto.setMessage("删除失败");
			return rtDto;
		}
		rtDto.setType(rtDto.SUCCESS);
		return rtDto;
	}
	/**
	 * 导入时间
	 * @param request
	 * @param response
	 * @param path
	 * @return
	 */
	@RequestMapping("/inputhours")
	@ResponseBody
	private Object inputHours(HttpServletRequest request,
			HttpServletResponse response,String path){
		String upath = request.getSession().getServletContext()
				.getRealPath("/");
		String filepath=upath+path;
		ResultDto rtDto = new ResultDto();
//		Workbook workbook = ExcelUtil.getWorkbook(filepath);
//		Sheet sheet=workbook.getSheet(0);
		try {
			List<Object[]> list=ReadExcel.readExcel(filepath);
			for(Object[] obj:list){
				OaDictNotStdManHoursCfg oadh =new OaDictNotStdManHoursCfg();
				oadh.setCreateTime(new Date());
				oadh.setManHoursTypeId(1);
				oadh.setYear(2017);
				oadh.setManHoursTypeId(1);
				oadh.setNote("1111");
				oadh.setName("1111");
				oadh.setManHoursTypeName("非标准工时制");
				oadh.setMonth(7);
				oadh.setOffDutyTime(new Date());
				oadh.setOnDutyTime(new Date());
				oadh.setOperator(1);
				oadh.setCode("333333");
				oadh.setStatus(1);
				oaDictNotStdManHoursCfgService.create(oadh);
			}
			rtDto.setType(rtDto.SUCCESS);
			System.out.println(list.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rtDto.setType(rtDto.ERROR);
		}
		return rtDto;
	}
	
	@RequestMapping("/userlist")
	@ResponseBody
	private Object userlist(HttpServletRequest request,
			HttpServletResponse response){
		DataGridModel param =new DataGridModel();
		//DataGridSortModel dataGridSortModel=new DataGridSortModel();
		//param.getSortBy().s
		DataGridSortModel dgsm = new DataGridSortModel("id", "ASC");
		param.getSortBy().add(dgsm);
		List<SysUser> list=sysUserService.doListByCondition(param);
		return list;
	}
	
	
}
